package swd.work.generator.v1.java.base;

import java.io.*;

import com.pub.database.*;
import com.pub.tools.*;
import swd.base.SwdBase;

public class BeanExtend extends SwdBase {
    //public QueryBean gqbnDAO = null; //這麼做可以只用到一個transaction
    //public SwdFunction gswdFun = null;

    protected final String gstrTabChar = "   "; //要使用何種字元當內縮字元ex." ","\t",etc,.
    protected StringBuffer gstbTabStr = new StringBuffer(); //共內縮幾個gstrTabChar

    public String gstrUserNO; //要取得那一組環境參數
    public String gstrSysNO;

    protected BeanExtend(QueryBean pqbnDAO, String pstrUserNO) throws Exception {
        super(null,null,pqbnDAO);
        //gswdFun = new SwdFunction(gqbnDAO);
        gstrUserNO = pstrUserNO;
        gstrSysNO = super.getSysNO(gstrUserNO).toLowerCase();
    }

    /**
     *
     * @param pbwrPrgNO BufferedWriter 輸出物件
     * @param pstrSysNO String 系統編號
     * @param pstrPrgNO String 程式編號
     * @param pstrBeanName String 要產生的JavaBean 名稱
     * @param pstbTabStr StingBuffer 縮排字集
     * @param pstrTabChar String 縮排字元
     * @return String
     */
    protected String outputBeanHeader(BufferedWriter pbwrPrgNO,
                                      String pstrSysNO, String pstrPrgNO,
                                      String pstrBeanName,
                                      StringBuffer pstbTabStr,
                                      String pstrTabChar) throws Exception {
        try {
            outputJavaCode(pbwrPrgNO, pstbTabStr, pstrTabChar,
                           "package com.swd." + pstrSysNO + "." + pstrPrgNO +
                           ";", 0);

            outputJavaCode(pbwrPrgNO, pstbTabStr, pstrTabChar,
                           "import com.pub.database.*;", 2);
            outputJavaCode(pbwrPrgNO, pstbTabStr, pstrTabChar,
                           "import java.util.*;", 1);
            outputJavaCode(pbwrPrgNO, pstbTabStr, pstrTabChar,
                           "import javax.servlet.http.*;", 1);
            outputJavaCode(pbwrPrgNO, pstbTabStr, pstrTabChar,
                           "import floweng.*;", 1);
            outputJavaCode(pbwrPrgNO, pstbTabStr, pstrTabChar,
                           "import floweng.function.*;", 1);
            outputJavaCode(pbwrPrgNO, pstbTabStr, pstrTabChar,
                           "import com.pub.tools.*;", 1);
            outputJavaCode(pbwrPrgNO, pstbTabStr, pstrTabChar,
                           "import com.pub.web.*;", 1);

            outputJavaCode(pbwrPrgNO, pstbTabStr, pstrTabChar,
                           "public class " +
                           pstrBeanName.substring(0, pstrBeanName.indexOf(".")) +
                           " {", 2);

            return "successfull";
        } catch (Exception e) {
            throw new Exception(e.toString());
        }
    }

    /**
     * outputJavaCode
     *
     * @param pbwrOutput BufferedWriter 輸出物件
     * @param pstbTabString StringBuffer 縮排字串
     * @param pstrTabChar String 組成組排字串的字元
     * @param pstrJavaCode String 要輸出的Java code
     * @param pintNewLine integer 輸出所需的換行數
     */
    protected void outputJavaCode(BufferedWriter pbwrOutput,
                                  StringBuffer pstbTabString,
                                  String pstrTabChar, String pstrJavaCode,
                                  int pintNewLineCount) throws Exception {
        try {
            for (int i = 1; i <= pintNewLineCount; i++) {
                pbwrOutput.newLine();
            }

            int lintBeginSingleCnt = PublicFunction.getDesStrCount(pstrJavaCode,
                    "{");
            int lintEndSingleCnt = PublicFunction.getDesStrCount(pstrJavaCode,
                    "}");

            if (pstbTabString != null && ((lintBeginSingleCnt < lintEndSingleCnt) || (pstrJavaCode.indexOf("{")>-1 && pstrJavaCode.indexOf("}")>-1))) { //判斷是否凸排
                pstbTabString.delete(0, pstrTabChar.length());
            }

            if (pstbTabString != null) {
                pbwrOutput.write(pstbTabString.toString()); //先輸出縮排字串
            }

            pbwrOutput.write(pstrJavaCode); //輸出Java code

            if (lintBeginSingleCnt > lintEndSingleCnt  || (pstrJavaCode.indexOf("{")>-1 && pstrJavaCode.indexOf("}")>-1)) { //判斷是否凹排
                pstbTabString.append(pstrTabChar);
            }
        } catch (Exception e) {
            throw new Exception(e.toString());
        }
    }


}
